<?php
class table extends Zend_Db_Table_Abstract
{
	static $db;
	public function getCount($where = null)
	{
		$table = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
		if($where) {
			$where = (array) $where;
			foreach ($where as $key=>&$val) {
				if(is_int($val)){
					$val = "`".$key."` = ".$val;
				} else if (is_array($val)){
					$val = "`".$key."` in (".join(',',$val).") ";
				} else if (!is_string($key)){
					$val = $val;
				} else{
					$val = "`".$key."` = '".$val."'";
				}
			}
			$where = implode(' AND ', $where);
		}
		//adver="sf"
		/**
		   * Build the SQL statement
		   */
		$sql = "SELECT COUNT(1) FROM " . $this->_db->quoteIdentifier($table, true)
		. (($where) ? " WHERE $where" : '');
		return $this->_db->fetchOne($sql);
	}
	
	protected static function db($config=null)
	{
		if (empty($config))
		{
			require_once ROOT. 'libs/Zend/Registry.php';
			$config = Zend_Registry::get('config');
		}	

		$dbId = md5(join('|',$config));

		if (empty(self::$db[$dbId]))
		{
			require_once ROOT. 'libs/Zend/Db.php';
            self::$db[$dbId] = Zend_Db::factory('mysqli',$config);
            empty($config['charset']) or self::$db[$dbId]->query("SET NAMES '{$config['charset']}'");
		}
		return self::$db[$dbId];
	}
	
	public function fetchAll($where=null,$order=null,$count=null,$offset=null)
	{
		if (is_array($where))
		{
			$db	= $this->getAdapter();
			foreach ($where as $key=>$val)
			{
				if (!is_string($key)) $w[] = $val;
				else $w[]	= is_array($val) ? $db->quoteInto("$key in (?)",$val) : $db->quoteInto("$key=?",$val);
			}
			$where	= join(' AND ',$w);
		}
		#if (DEBUG) var_dump($where);
		#if ($this->getCache(md5($where.$order.$count.$offset)) !== false)
		#{
		#	return $this->getCache(md5($where.$order.$count.$offset));
		#} 
		#else 
		#{
			$data	= parent::fetchAll($where,$order,$count,$offset);
			$data	= method_exists($data, 'toArray') ? $data->toArray() : $data;
		#	$this->setCache(md5($where.$order.$count.$offset), $data);
			return $data;
		#}
	}
	
	public function fetchRow($where=null,$order=null)
	{
		if (is_array($where))
		{
			$db	= $this->getAdapter();
			foreach ($where as $key=>$val)
			{
				$w[]	= is_array($val) ? $db->quoteInto("$key in (?)",$val) : $db->quoteInto("$key=?",$val);
			}
			$where	= join(' AND ',$w);
		}

		$data	= parent::fetchRow($where,$order);	
		return method_exists($data, 'toArray') ? $data->toArray() : $data;
	}
	
	public function update($data,$where)
	{
		if (is_array($where))
		{
			$db	= $this->getAdapter();
			foreach ($where as $key=>$val)
			{
				$w[]	= is_array($val) ? $db->quoteInto("$key in (?)",$val) : $db->quoteInto("$key=?",$val);
			}
			$where	= join(' AND ',$w);
		}
		$data	= array_get($data,$this->fields);
		return parent::update($data, $where);
	}
	
	public function insert($data)
	{
		$data	= array_get($data, $this->fields);
		parent::insert($data);
		return $this->getAdapter()->lastInsertId();
	}
	
	public function delete($where)
	{
		if (is_array($where))
		{
			$db	= $this->getAdapter();
			foreach ($where as $key=>$val)
			{
				$w[]	= is_array($val) ? $db->quoteInto("$key in (?)",$val) : $db->quoteInto("$key=?",$val);
			}
			$where	= join(' AND ',$w);
		}

		$data	= parent::delete($where);	
		return $data;
	}
}